package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ClassifyDetilsBean;
import com.wnhz.work.yz.utils.DisplayUtil;
import com.wnhz.work.yz.view.Tag;
import com.wnhz.work.yz.view.TagListView;
import com.wnhz.work.yz.view.TagView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by admin on 2017/1/18.
 */
public class MultipleSelectAdapter extends BaseAdapter {
    private Context mContext;
    private List<ClassifyDetilsBean> skuList;
    private int index;
    private Map<String, String> map = new HashMap<>();

    public MultipleSelectAdapter(Context context, List<ClassifyDetilsBean> skuList) {
        this.mContext = context;
        this.skuList = skuList;
    }

    @Override
    public int getCount() {
        return skuList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MultipleSelectViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new MultipleSelectViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.mutiple_select_item, null);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tlv_tagview = (TagListView) convertView.findViewById(R.id.tlv_tagview);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MultipleSelectViewHolder) convertView.getTag();
        }

        viewHolder.tv_title.setText(skuList.get(position).getSkuName());

        final List<Tag> standard_tagList = new ArrayList<>();
        Collections.reverse(skuList.get(position).getData());
        //遍历集合拿到名称和sku_id  选中状态为否
        for (int i = 0; i < skuList.get(position).getData().size(); i++) {
            Tag tag = new Tag();
            tag.setTitle(skuList.get(position).getData().get(i).getName());
            tag.setId(Integer.parseInt(skuList.get(position).getData().get(i).getSku_id()));
            if (i == 0) {
                tag.setChecked(true);
            } else {
                tag.setChecked(false);
            }
            standard_tagList.add(tag);
        }

        Log.e("=standard_tagList集合的长度=", "" + standard_tagList.size());

        viewHolder.tlv_tagview.setTags(standard_tagList);

        for (int i = 0; i < standard_tagList.size(); i++) {
            TagView view = (TagView) viewHolder.tlv_tagview.getChildAt(i);
            Tag tag = standard_tagList.get(i);
            if (standard_tagList.get(i).isChecked()) {
                index = tag.getId();
                Log.e("==默认选中==", position + " ==下标 index== " + index);
                map.put(position + "", index + "");
                view.setBackgroundDrawable(pitchOnColor(mContext.getResources().getColor(R.color.main_color), mContext.getResources().getColor(R.color.main_color)));
                view.setTextColor(mContext.getResources().getColor(R.color.white));
            } else {
                view.setBackgroundDrawable(pitchOnColor(mContext.getResources().getColor(R.color.white), mContext.getResources().getColor(R.color.text)));
                view.setTextColor(mContext.getResources().getColor(R.color.text));
            }
        }

        final MultipleSelectViewHolder finalViewHolder = viewHolder;

        viewHolder.tlv_tagview.setOnTagClickListener(new TagListView.OnTagClickListener() {
            @Override
            public void onTagClick(TagView tagView, Tag tag) {
                if (tagClick != null) {
                    int index = tag.getId();
                    tagClick.onClick(position, index, tagView, standard_tagList, tag, finalViewHolder.tlv_tagview);
                }
            }
        });

        return convertView;
    }

    class MultipleSelectViewHolder {
        TextView tv_title;
        TagListView tlv_tagview;
    }

    public interface tagListView {
        void onClick(int position, int index, TagView tagView, List<Tag> standard_tagList, Tag tag, TagListView tlv_tagview);
    }

    public void setTagClick(tagListView tagClick) {
        this.tagClick = tagClick;
    }

    public tagListView tagClick;

    private GradientDrawable pitchOnColor(int color1, int color2) {
        GradientDrawable gd = new GradientDrawable();
        int strokeWidth = DisplayUtil.dp2px(mContext, 0.5f);//边框宽度
        //int roundRadius = DensityUtils.dp2px(this, 25); // 8dp 圆角半径

        gd.setCornerRadius(DisplayUtil.dp2px(mContext, 4));
        gd.setColor(color1);
        //gd.setCornerRadius(roundRadius);
        gd.setStroke(strokeWidth, color2);
        return gd;
    }


    public Map<String, String> getSkuAttr() {
        return map;
    }
}
